package com.example.tomcatdemo;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatConfig {

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

        // 添加HTTP端口（可选）
        factory.addAdditionalTomcatConnectors(createHttpConnector());

        // 自定义错误页面（示例）
        // factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));

        return factory;
    }

    private Connector createHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8081); // 额外HTTP端口
        return connector;
    }
}